<?php

namespace app\helpers;

use MobTech\MobPush\Builder\PushWorkBuilder;
use MobTech\MobPush\Config\MobPushConfig;
use MobTech\MobPush\Client\Push\PushV3Client;

class Push {
    const ID_PREFIX = 'travelpig';
    const JPUSH_APP_KEY = 'a036bb4fbf1ed75048273753';
    const JPUSH_MASTER_SECRET = '47006786b0b4a853cb8f022d';

    const TYPE_NO_MESSAGE = 0;
    const TYPE_REFRESH_USERINFO = 1;

    public static function push($title,$content,$alias,$type = self::TYPE_NO_MESSAGE,$url = '')
    {
        $mobPushConfig = new MobPushConfig();
        $mobPushConfig::$appkey = \Yii::$app->params['mob']['appKey'];
        $mobPushConfig::$appSecret = \Yii::$app->params['mob']['appSecret'];

        $extra = [];

        $pushWorkBuilder = new PushWorkBuilder();

        if($type == self::TYPE_REFRESH_USERINFO){
            $extra['refresh_user'] = '1';
        }

        if($url){
            $extra['url'] = $url;
        }

//        (new PushV3Client())->pushAll('', $title, $content, ...$alias);

        $builder = PushV3Client::pushTaskV3($pushWorkBuilder
            ->setTargetByAlias(Str::getOutTradeNo(), $title, $content, $alias)
            ->setNotifyExtraMap($extra)
            ->builder()
        );


//        $client = new Client(self::JPUSH_APP_KEY,self::JPUSH_MASTER_SECRET);
//
//        try {
//            $response = $client->push()
//                ->setPlatform('all')
//                //->addAllAudience()
//                ->addAlias($alias)
//                ->setNotificationAlert($title)
//                ->iosNotification([
//                    'title' => $title, 'subtitle' => $content,
//                    "body" => Date("Y-m-d G:i:s"), "content-availabe" => 1
//                ], [
//                    'sound' => 'sound.caf',
//                    'category' => $title,
//                    'content-available' => true,
//                    'mutable-content' => true,
//                    'extras' => [
//                        'bussiness_type'=>$type,
//                    ]
//                ])
//                ->androidNotification($content, [
//                    'style' => 1,
//                    'alert_type' => 1,
//                    'title' => $title,
//                    'extras' => [
//                        'bussiness_type'=>$type,
//                    ]
//                ])
//                ->message($content, [
//                    'title' => $title,
//                    'content_type' => 'text',
//                    'extras' => [
//                        'bussiness_type'=>$type,
//                    ]
//                ])
//                ->options([
//                    'apns_production' => true,
//                ])
//                ->send();
//            return 1;
//        }catch (\JPush\Exceptions\APIConnectionException $e){
//            return 0;
//        }catch(\JPush\Exceptions\APIRequestException $e){
//            return 0;
//        }
    }
}